package com.yhny.lykj.yuhannongye.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longsichao.lscframe.ctrl.DataManager;
import com.longsichao.lscframe.model.LSCModel;
import com.longsichao.lscframe.view.LSCToast;
import com.yhny.lykj.yuhannongye.R;
import com.yhny.lykj.yuhannongye.app.BaseActivity;
import com.yhny.lykj.yuhannongye.app.LSCLinearLayoutManager;
import com.yhny.lykj.yuhannongye.app.UserConfig;
import com.yhny.lykj.yuhannongye.model.BaseModel;
import com.yhny.lykj.yuhannongye.model.CityListModel;
import com.yhny.lykj.yuhannongye.net.NetHelper;
import com.yhny.lykj.yuhannongye.tools.CodeScan;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;

public class EditUserActivity extends BaseActivity {
    EditText edit;
    TextView rightTv;
    TextView scan_in;
    String text = "";
    String Merid = "";
    int tag;
    RecyclerView leftListView;
    RelativeLayout viewGroup;
    private TextView tipTv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_user);
        text = getIntent().getStringExtra("edit");
        tag = getIntent().getIntExtra("editid",0);
        Merid = getIntent().getStringExtra("merId");
        TextView topName = (TextView)findViewById(R.id.name_top_bar);
        topName.setText(text);
        findViewById(R.id.left_group).setOnClickListener(this);
        findViewById(R.id.left_text).setOnClickListener(this);
        scan_in = (TextView)findViewById(R.id.scan_in);
        scan_in.setOnClickListener(this);
        rightTv = (TextView)findViewById(R.id.right_text);
        rightTv.setText("修改");
        rightTv.setVisibility(View.VISIBLE);
        rightTv.setOnClickListener(this);
        edit= (EditText)findViewById(R.id.edit);
        viewGroup = (RelativeLayout)findViewById(R.id.refresh_list);
        if(tag==1003){
            //绑定会员卡
            edit.setVisibility(View.VISIBLE);
            viewGroup.setVisibility(View.GONE);
            scan_in.setVisibility(View.GONE);
        }else if(tag==1006){
            //推荐人
            edit.setVisibility(View.VISIBLE);
            viewGroup.setVisibility(View.GONE);
            scan_in.setVisibility(View.GONE);
        }else if(tag ==1007){
            //入驻商家
            edit.setVisibility(View.VISIBLE);
            viewGroup.setVisibility(View.GONE);
            scan_in.setVisibility(View.VISIBLE);
            edit.setText(Merid);
        }else{
            edit.setVisibility(View.VISIBLE);
            viewGroup.setVisibility(View.GONE);
            scan_in.setVisibility(View.GONE);
        }

    }

    @Override
    public boolean onModel(int caseId, LSCModel model) {
        if (super.onModel(caseId, model)) {
            dismissProgressDialog();
            return false;
        }
        switch (caseId){

            case MODEL_PROCESS_1:{
                BaseModel baseModel = (BaseModel)model;
                if (model == null || model.equals("")){
                    LSCToast.show(this,"绑定失败");
                }else if (baseModel.status!=NET_SUSSEC){
                    LSCToast.show(this,baseModel.msg);
                }else{
                    LSCToast.show(this,"绑定成功");
                    String temp = edit.getText().toString();
                    Intent intent = new Intent(this,UserInfoActivity.class);
                    intent.putExtra("editid",temp);
                    setResult(tag, intent);
                    EditUserActivity.this.finish();
                }

                break;

            }
            case MODEL_PROCESS_2:{
                BaseModel baseModel = (BaseModel)model;
                if (model == null || model.equals("")){
                    LSCToast.show(this,"绑定失败");
                }else if (baseModel.status!=NET_SUSSEC){
                    LSCToast.show(this,baseModel.msg);
                }else{
                    LSCToast.show(this,"绑定成功");
                    String temp = edit.getText().toString();
                    Intent intent = new Intent(this,UserInfoActivity.class);
                    intent.putExtra("editid",temp);
                    setResult(tag, intent);
                    EditUserActivity.this.finish();
                }
                break;
            }
            case MODEL_PROCESS_3:{
                BaseModel baseModel = (BaseModel)model;
                if (model == null || model.equals("")){
                    LSCToast.show(this,"绑定失败");
                }else if (baseModel.status!=NET_SUSSEC){
                    LSCToast.show(this,baseModel.msg);
                }else{
                    LSCToast.show(this,"绑定成功");
                    String temp = edit.getText().toString();
                    UserConfig.setShopNum(temp);
                    Intent intent = new Intent(this,UserInfoActivity.class);
                    intent.putExtra("editid",temp);
                    setResult(tag, intent);
                    EditUserActivity.this.finish();
                }
                break;
            }
        }
        dismissProgressDialog();
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String scanResult = CodeScan.onActivityResult(requestCode, resultCode, data);
        if (scanResult != null && !scanResult.isEmpty()) {
            //扫码绑定商家
            edit.setText(""+scanResult);
            DataManager.getData(MODEL_PROCESS_3, NetHelper.User.setMemberMer(scanResult), this);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.scan_in:{
                //归属商家 扫码
                if (TextUtils.isEmpty(UserConfig.getConfigUserId())){
                    //登录
                    Intent intent = new Intent(this,LoginActivity.class);
                    startActivity(intent);
                }else {
                    CodeScan.startCodeScan(this);
                }
                break;
            }
            case R.id.left_text:
            case R.id.left_group:{
                finish();
                break;
            }
            case R.id.right_group:
            case R.id.right_text:{
                if (tag==1003){
                    String temp = edit.getText().toString();
                    DataManager.getData(MODEL_PROCESS_1, NetHelper.User.getBindCard(temp), this);
                }else if(tag==1006){
                    //推荐人
                    String temp = edit.getText().toString();
                    DataManager.getData(MODEL_PROCESS_2, NetHelper.User.setMemberMem(temp), this);

                }else if(tag==1007){
                    //归属商家
                    String temp = edit.getText().toString();
                    DataManager.getData(MODEL_PROCESS_3, NetHelper.User.setMemberMer(temp), this);
                }else{
                    String temp = edit.getText().toString();
                    Intent intent = new Intent(this,UserInfoActivity.class);
                    intent.putExtra("editid",temp);
                    setResult(tag, intent);
                    finish();
                }

                break;
            }
        }
        super.onClick(v);
    }
}
